/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import core.MediaManager;
import core.ScreenEngine;
import core.Settings;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class SettingsForm extends Form
{
    private static final int SAVE_ID = 1;
    
    private TextField login;
    private TextField password;
    private CheckBox saveLoginPass;
    private TextField host;
    private TextField secondHost;
    
    private ComboBox newOrderMelodySelect;
    private ComboBox startOrderMelodySelect;
    private ComboBox cancelOrderMelodySelect;
    private ComboBox connectionErrorMelodySelect;
    private ComboBox messageMelodySelect;
    
    private ComboBox fontSizeSelect;
    
    private Command save;
    
    public SettingsForm()
    {
        super("settings");

        init();
    }
    
    
    private void init() 
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        
        //Создаем компоненты
        //Логин
        addComponent(new Label("login"));
        login = new TextField();
        login.setSelectCommandText("");
        login.setConstraint(TextArea.ANY);
        login.setInputModeOrder(new String[]{"Abc","ABC","abc","123"});
        addComponent(login);

        //Пароль
        addComponent(new Label("pass"));
        password = new TextField();
        password.setSelectCommandText("");
        password.setConstraint(TextArea.ANY | TextArea.PASSWORD);
        password.setInputModeOrder(new String[]{"Abc","ABC","abc","123"});
        addComponent(password);

        //Запоминать ли логин-пароль
        saveLoginPass = new CheckBox("remember_data");
        addComponent(saveLoginPass);

        //Сервер
        host = new TextField();
        host.setSelectCommandText("");
        host.setConstraint(TextArea.ANY);
        host.setInputModeOrder(new String[]{"Abc","ABC","abc","123"});
        /*addComponent(new Label("host"));
        addComponent(host);*/

        //Резервный сервер
        secondHost = new TextField();
        secondHost.setSelectCommandText("");
        secondHost.setConstraint(TextArea.ANY);        
        secondHost.setInputModeOrder(new String[]{"Abc","ABC","abc","123"});
        /*addComponent(new Label("second_host"));
        addComponent(secondHost);*/
        
        //Компоненты выбора мелодии
        newOrderMelodySelect = new ComboBox();
        newOrderMelodySelect.setSelectCommandText("");
        startOrderMelodySelect = new ComboBox();
        startOrderMelodySelect.setSelectCommandText("");
        cancelOrderMelodySelect = new ComboBox();
        cancelOrderMelodySelect.setSelectCommandText("");
        connectionErrorMelodySelect = new ComboBox();
        connectionErrorMelodySelect.setSelectCommandText("");
        messageMelodySelect = new ComboBox();
        messageMelodySelect.setSelectCommandText("");
        //Заполням доступными мелодиями
        for (int i = 0; i < MediaManager.SOUNDS.length; ++i)
        {
            newOrderMelodySelect.addItem(MediaManager.SOUNDS[i].getValue());
            startOrderMelodySelect.addItem(MediaManager.SOUNDS[i].getValue());
            cancelOrderMelodySelect.addItem(MediaManager.SOUNDS[i].getValue());
            connectionErrorMelodySelect.addItem(MediaManager.SOUNDS[i].getValue());
            messageMelodySelect.addItem(MediaManager.SOUNDS[i].getValue());
        }
        ActionListener playMelodyAction = new ActionListener() 
        {
            public void actionPerformed(ActionEvent evt) 
            {
                MediaManager.getManager().playMusic(((ComboBox)evt.getComponent()).getSelectedIndex());
            }
        };
        newOrderMelodySelect.addActionListener(playMelodyAction);
        startOrderMelodySelect.addActionListener(playMelodyAction);
        cancelOrderMelodySelect.addActionListener(playMelodyAction);
        connectionErrorMelodySelect.addActionListener(playMelodyAction);
        messageMelodySelect.addActionListener(playMelodyAction);
       
        //Добавляем компоненты
        addComponent(new Label("new_ord_mel"));
        addComponent(newOrderMelodySelect);
        addComponent(new Label("start_ord_mel"));
        addComponent(startOrderMelodySelect);
        addComponent(new Label("cancel_ord_mel"));
        addComponent(cancelOrderMelodySelect);
        addComponent(new Label("connection_err"));
        addComponent(connectionErrorMelodySelect);
        addComponent(new Label("message_mel"));
        addComponent(messageMelodySelect);
        
        //Компонент выбора размера шрифта
        fontSizeSelect = new ComboBox();
        fontSizeSelect.setSelectCommandText("");
        fontSizeSelect.addItem("small");
        fontSizeSelect.addItem("middle");
        fontSizeSelect.addItem("big");
        addComponent(new Label("font_size"));
        addComponent(fontSizeSelect);
                
        BackCommand cancel = new BackCommand();
        cancel.setCommandName("cancel");
        addCommand(cancel);
        
        save = new Command("ok",SAVE_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                //Сохраняем настроки
                saveSettings();

                //Возвращаемся на предыдущий экран
                ScreenEngine.getEngine().pop(true);

                evt.consume();
            }
        };
        addCommand(save);
    }
    
    public void show()
    {
        //Устанавливаем текущие настройки
        setSettings();

        //Отображае форму
        super.show();
    }
    
    private void setSettings()
    {
        Settings settings = Settings.getSettings();
        
        login.setText(settings.getLogin());
        password.setText(settings.getPassword());
        saveLoginPass.setSelected(settings.isSaveLoginPass());
        host.setText(settings.getHost());
        secondHost.setText(settings.getSecondHost());
        
        newOrderMelodySelect.setSelectedIndex(settings.getNewOrderMelodyId());
        startOrderMelodySelect.setSelectedIndex(settings.getStartOrderMelodyId());
        cancelOrderMelodySelect.setSelectedIndex(settings.getCancelOrderMelodyId());
        connectionErrorMelodySelect.setSelectedIndex(settings.getConnectionErrorMelodyId());
        messageMelodySelect.setSelectedIndex(settings.getMessageMelodyId());
        
        switch(settings.getFontSize())
        {
            case Settings.FS_SMALL : fontSizeSelect.setSelectedIndex(0); break;
            case Settings.FS_MIDDLE : fontSizeSelect.setSelectedIndex(1); break;
            case Settings.FS_BIG : fontSizeSelect.setSelectedIndex(2); break;
        }
    }
    
    private void saveSettings()
    {
        Settings settings = Settings.getSettings();
        
        settings.setLogin(login.getText());
        settings.setPassword(password.getText());
        settings.setSaveLoginPass(saveLoginPass.isSelected());
        settings.setHost(host.getText());
        settings.setSecondHost(secondHost.getText());
        settings.setNewOrderMelodyId(newOrderMelodySelect.getSelectedIndex());
        settings.setStartOrderMelodyId(startOrderMelodySelect.getSelectedIndex());
        settings.setCancelOrderMelodyId(cancelOrderMelodySelect.getSelectedIndex());
        settings.setConnectionErrorMelodyId(connectionErrorMelodySelect.getSelectedIndex());
        settings.setMessageMelodyId(messageMelodySelect.getSelectedIndex());
       
        switch(fontSizeSelect.getSelectedIndex())
        {
            case 0 : settings.setFontSize(Settings.FS_SMALL); break;
            case 1 : settings.setFontSize(Settings.FS_MIDDLE); break;
            case 2 : settings.setFontSize(Settings.FS_BIG); break;
        }
        
        //сохраняем
        settings.save();        
    }
}
